package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.Activity;
import com.zpp.wedding_admin.entity.vo.ActivityQuery;
import com.zpp.wedding_admin.service.IActivityService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(value = "活动管理")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/activity")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @ApiOperation(value = "获取所有活动")
    @GetMapping("/getAll")
    public R getAll() {
        List<Activity> activityList = activityService.list(null);
        return R.ok().data("activityList", activityList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("getActivityPageVo/{currentPage}/{size}")
    public R getBannerPageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {

        Page<Activity> page = new Page<Activity>(currentPage, size);
        activityService.page(page, null);
        List<Activity> activityList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("activityList", activityList).data("total", total);
    }

    @ApiOperation(value = "分页条件查询活动列表")
    @PostMapping("getActivityPageQuery/{currentPage}/{size}")
    public R getBannerPageQuery(@PathVariable Long currentPage,
                                @PathVariable Long size,
                                @RequestBody ActivityQuery activityQuery) {
        // RequestBody将json转换为实体类
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<Activity>();
        String activityName = activityQuery.getActivityName();
        lqw.like(!StringUtils.isEmpty(activityName), Activity::getActivityName, activityName);
        Page<Activity> page = new Page<Activity>(currentPage, size);
        activityService.page(page, lqw);
        List<Activity> activityList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("activityList", activityList).data("total", total);
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/deleteAllSelect")
    public R deleteAllSelect(@RequestBody Integer iDs[]) {
        boolean b = activityService.deleteAllSelect(iDs);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id删除一个")
    @DeleteMapping("/deleteOneById/{id}")
    public R deleteOneById(@PathVariable Integer id) {
        boolean b = activityService.removeById(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id获取活动信息")
    @GetMapping("/getOneById/{id}")
    public R getOneById(@PathVariable Integer id) {
        Activity activity = activityService.getById(id);
        if ( activity != null ) return R.ok().data("activity", activity);
        return R.error();
    }

    @ApiOperation(value = "更新或添加")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody Activity activity) {
        boolean b = activityService.saveOrUpdate(activity);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "模糊查询名称")
    @GetMapping("/searchByName/{activityName}")
    public R searchByName(@PathVariable String activityName) {
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<Activity>();
        lqw.like(Activity::getActivityName, activityName);
        List<Activity> activityList = activityService.list(lqw);
        return R.ok().data("activityList", activityList);
    }

    @ApiOperation(value = "结束活动")
    @PutMapping("/finishActivity/{id}")
    public R finishActivity(@PathVariable Integer id) {
        boolean b = activityService.updateFinish(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "参与活动人数加一")
    @GetMapping("activityAdd/{id}")
    public R activityAdd(@PathVariable Integer id) {
        boolean b = activityService.activityAdd(id);
        if ( b ) return R.ok();
        return R.error();
    }
}
